/**
 * Tree
 * -------------------------------------------------
 * 
 * - XML Tree exibition
 * 
 * @author Aryadne Guardieiro P. Rezende
 */
package view;

import java.io.IOException;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Tree extends DefaultHandler {

	private JTree tree;
	private String documentName;
	private DefaultMutableTreeNode noRoot;
	private DefaultMutableTreeNode lastNode;
	private String document = new String();

	private void createTree() {
		noRoot = new DefaultMutableTreeNode("xml: "+documentName);
		tree = new JTree(noRoot);
		lastNode = noRoot;
	}

	private void addSon(String leaf) {
		DefaultMutableTreeNode l = new DefaultMutableTreeNode(leaf);
		lastNode.add(l);
		tree = new JTree(noRoot);
		lastNode = l;
	}
		
	public void startDocument() {
		createTree();
	}

	public JTree createTree(String uri) throws SAXException, IOException {
		
		SAXParser sp = new SAXParser();
		InputSource source = new InputSource(uri);
		sp.setContentHandler(this);
		
		documentName = uri;
		sp.parse(source);		
		return tree;
	}
	
	public void endDocument() { }

	/**
	 * This function read the start element of tag from xml This function is
	 * implemented of SAXParser
	 * 
	 * @param String nameSpaceURI
	 * @param String localNome
	 * @param String rawName its name of tag XML
	 * @oaram Atributes atters
	 * @return void
	 */
	public void startElement(String namespaceURI, String localName, String rawName, Attributes attrs) {		
		addSon(rawName);
		int numAt = attrs.getLength();
		
		document += "<" + rawName;
		
		if (numAt > 0)
			addAttrs(attrs, numAt);				
		
		document += ">";
	}

	public void addAttrs(Attributes attrs, int numAt) {		
		for(int i = 0; i < numAt; i++) {
			DefaultMutableTreeNode l = new DefaultMutableTreeNode("@"+(attrs.getLocalName(i))+" : " + attrs.getValue(i));			
			lastNode.add(l);
			tree = new JTree(noRoot);
			document += " " + attrs.getLocalName(i) +"=" + attrs.getValue(i);
		}
	}
	
	/**
	 * This function clean the characteres
	 * @return void
	 */	
	public void ignorableWhitespace(char buffer[], int offset, int length) {}
	
	public void characters(char ch[], int start, int length) {
		String n = new String(ch, start, length);
		
		if (!white(n)){
			DefaultMutableTreeNode l = new DefaultMutableTreeNode("value: "+n);
			lastNode.add(l);
			tree = new JTree(noRoot);			
		}
		
		document += " " + n + "";
		
		return;		
	}
	
	public boolean white(String n){
		int flag = 0;
		for(int i = 0; i < n.length(); i++) {
			char c = n.charAt(i);
			if (c == ' ' || c == '\n' || c == '\t')
				flag++;
		}
		
		if (flag == n.length())
			return true;
		return false;
	}

	/**
	 * This function read the end element of tag from xml This function is
	 * implemented of SAXParser
	 * @param String nameSpaceURI
	 * @param String localNome
	 * @param String rawName its name of tag XML
	 * @return void
	 */
	public void endElement(String namespaceURI, String localName, String rawName) {
		lastNode = (DefaultMutableTreeNode) lastNode.getParent();
		
		document += "<" + rawName + ">";
	}

	public void setDocument(String document) {
		this.document = document;
	}

	public String getDocument() {
		return document;
	}
}